package my.game.Descension;

import java.io.BufferedReader;
import java.io.FileReader;

public class LevelLoader implements Constants{

	String nextline;
	boolean mapLoaded;
	String doorLocation; // Where door takes player to.
	String door2Location; // Where cave door takes player to. Only on maps with 2 doors.
						  // Which is overworld and the first cave map.
	String info; // Info for a tile...
	private int solidity[];
	private Sprite tiles[];
	
	public LevelLoader(String level) throws Exception {
		solidity = new int[400];
		tiles = new Sprite[400];
		char mapString[] = new char[400];
		
		mapLoaded = false;
		
		String map = "levels/"+level + ".txt";
		
		for (int i = 0; i < 400; i++) {
			solidity[i] = 0;
			tiles[i] = null;
		}
		
		if (!mapLoaded) {
			BufferedReader mapBuffer = new BufferedReader(new FileReader(map));
			int inc = 0;
			for (int k = 0; k < 20; k++) {
				char foostring[] = mapBuffer.readLine().toCharArray();
				
				for (int j = 0; j < 20; j++) {
					mapString[inc] = foostring[j];
					inc++;
				}
			}
			
			String foostring = mapBuffer.readLine();
			
			doorLocation = foostring;
			
			foostring = mapBuffer.readLine();
			door2Location = foostring;
			
			mapBuffer.close();
		}
		
		for (int jk = 0; jk < mapString.length; jk++) {
			
			tiles[jk] = new Sprite("null"); // Default case;
			
			if (mapString[jk] == 'w') {
				tiles[jk] = new Sprite("water");
				tiles[jk].setPassable(false);
				
			}
			
			if (mapString[jk] == ' ') {
				tiles[jk] = new Sprite("grass");
				tiles[jk].setPassable(true);
			}
			
			if (mapString[jk] == 'g') {
				tiles[jk] = new Sprite("grass");
				tiles[jk].setPassable(true);
			}
			
			if (mapString[jk] == 't') {
				tiles[jk] = new Sprite("tree1");
				tiles[jk].setPassable(false);
			}
			
			// TODO: Unhardcode these so doors can go both ways.
			// TODO: Unhardcode Info
			if (mapString[jk] == 'd') {
				tiles[jk] = new Sprite("door");
				tiles[jk].setPassable(true);
				tiles[jk].setInfo("Town");
				tiles[jk].setDoor(true, doorLocation);
			}
			
			if (mapString[jk] == 'q') {
				tiles[jk] = new Sprite("skulls");
				tiles[jk].setPassable(false);
			}
			
			if (mapString[jk] == 'D') {
				tiles[jk] = new Sprite("cave_door");
				tiles[jk].setPassable(true);
				tiles[jk].setInfo("Cave Entrance");
				tiles[jk].setDoor(true, door2Location);
			}
			
			// TODO: Add AI to the sprites
			if (mapString[jk] == '$') {
				tiles[jk] = new Sprite("slug");
				tiles[jk].setPassable(false);
				
			}
			
		}
		
		int tally = 0;
		for (int l = 0; l < 20; l++) {
			for (int p = 0; p < 20; p++) {
				tiles[tally].setX(p);
				tiles[tally].setY(l);
				tally++;
			}
		}
		
	}
	
	
	public Sprite[] getTiles() {
		return tiles;
	}
	
	
	public Sprite findSprite(String door) {
		for (int i = 0; i < tiles.length; i++) {
			if(tiles[i].isDoor() && tiles[i].newLocation() == door)
			//if (tiles[i].newLocation() == door)
			//if (tiles[i].isDoor())
				return tiles[i];
		}
		Sprite nullSprite = new Sprite("null");
		//nullSprite.setDoor(true);
		
		return nullSprite;
	}
	
	// Returns sprite at specified coordinates
	public Sprite findSprite(int x, int y) {
		for (int i = 0; i < tiles.length; i++) {
			if(tiles[i].getX() == x && tiles[i].getY() == y)
				return tiles[i];
		}
		
		Sprite nullSprite = new Sprite("null");
		nullSprite.setPassable(false);
		return nullSprite;
	}
}
